import * as echarts from 'echarts';

let $echarts = {};
// debugger;

$echarts.$renderChart = async function getChart(vm, chartLsdConfigId, data) {
    // 注意，这里的this 指$echarts，不是vue实例
    const chartDom = document.getElementById(chartLsdConfigId);
    if (!chartDom) return;
    $echarts.$dispose(chartDom);
    const chart = echarts.init(chartDom, 'dark');
    // let chartJs;
    // chartJs = require(`../chart/lsd${chartLsdConfigId.substr(0, 4)}/lsd${chartLsdConfigId}`);
    
    let path = ''
    
    if(chartLsdConfigId) {
        const pathArr = chartLsdConfigId.split('-')
        path = `../chart/lsd-${pathArr[0]}/lsd-${pathArr[0]}-${pathArr[1]}.js`
    }
    // chartJs = import(path);
    // chartJs = import(`../chart/lsd${chartLsdConfigId.substr(0, 4)}/lsd${chartLsdConfigId}.js`);
    const { getChartOption, getChartEvents, registerWatchEvent } = await import(/* @vite-ignore */path);
    const option = await getChartOption(vm, data, chart);
    
    // 绑定echarts事件
    if (getChartEvents) {
        const chartEvents = getChartEvents(vm, data, { chart: chart, option: option });
        for (const action in chartEvents) {
            chart.on(action, chartEvents[action]);
        }
    }
    // 注册轮播联动监听事件
    registerWatchEvent && registerWatchEvent(vm, data, { chart: chart });

    // 默认背景透明
    if (!option.backgroundColor) option.backgroundColor = 'transparent';

    chart.setOption(option, true);
    return chart;
};

$echarts.$dispose = function dispose(chartDom) {
    const chart = echarts.getInstanceByDom(chartDom);
    if (chart) {
        chart.dispose();
    }
};

/**
 * 饼图隐藏数据为0的图例
 * @param {*数据来源} data
 * @param {*需要显示的key数组，如c1} key
 * @param  {...需要被删除某列的数组,如color数组} args
 */
$echarts.$dataFilterToZero = function (data, key = 'c1', ...args) {
    const allZero = data.every((item) => item[key] === 0);
    if (!allZero) {
        let i = 0;
        while (i < data.length) {
            const isZero = !data[i][key];
            if (isZero) {
                data.splice(i, 1);
                if (args.length) {
                    args.forEach((item) => {
                        item.splice(i, 1);
                    });
                }
            } else {
                i++;
            }
        }
    }
};

/**
 * 柱狀圖、堆叠图隐藏数据为0的图例
 * @param {*数据来源} source
 * @param {*需要显示的key数组，如[c1,c3]} keyList
 * @param  {...需要被删除某列的数组,如color数组} args
 */
$echarts.$stackHiddenZero = function stackHiddenZero(source, keyList, ...args) {
    let obj = {};
    let allZero = true;
    source.map((item) => {
        keyList.map((key) => {
            obj[key] ? (obj[key] += item[key] || 0) : (obj[key] = item[key] || 0);
            if (item[key]) {
                allZero = false;
            }
        });
    });
    if (allZero) {
        return;
    }
    const length = keyList.length;
    let i = 0;
    while (i < length) {
        if (obj[keyList[i]] === 0) {
            keyList.splice(i, 1);
            args.forEach((item) => {
                item.splice(i, 1);
            });
        } else {
            i++;
        }
    }
};

const install = function (app) {
    app.config.globalProperties.$echarts = $echarts;
};

export default install;
